package com.sm.service;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sm.model.SecondDirectory;
import com.sm.model.VideoCourse;
import com.sm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sm.dao.SecondDirectoryMapper;
import com.sm.dao.VideoCourseMapper;
import com.sm.model.CourseBean;

/**
 *	视频课程的二级类别
 * @author 60915
 */
@Service
public class VideoCourseService {
	
	@Autowired
	private SecondDirectoryMapper secondDirectoryMapper;
	@Autowired
	private VideoCourseMapper videoCourseMapper;
	
	
	/**
	 *	根据视频课程的一级类别id查询其所有的视频课程
	 * @param firstDirectoryId	视频课程的一级类别id
	 * @return
	 */
	public PageResult selectFirstAllVideo(Integer firstDirectoryId,Integer currentPage,Integer pageSize){
		PageHelper.startPage(currentPage,pageSize);
		//根据视频课程一级类别id查询其所有的视频课程
		Page<VideoCourse> videoCourses = videoCourseMapper.selectAllVideoByFirst(firstDirectoryId);
		return new PageResult(videoCourses.getTotal(),videoCourses);
	}
	
	
	/**
	 *	根据视频课程的一级类别id查询其所有的二级类别。
	 * @param firstDirectoryId	视频课程的一级类别id
	 * @return
	 */
	public List<SecondDirectory> selectSecondByFirstDirectory(Integer firstDirectoryId){
		//通过视频课程的一级类别id查询其包含的所有的二级类别。
		List<SecondDirectory> secondDirectorys = secondDirectoryMapper.selectSecondByFirstDirectory(firstDirectoryId);
		
		return secondDirectorys;
	}
	
	
	/**
	 *	根据视频课程的一级类别id查询其下的每个二级类别id查询其所有的视频课程。
	 * @param firstDirectoryId		视频课程的一级类别id
	 * @param secondDirectoryId		视频课程的二级类别id
	 * @return
	 */
	public PageResult selectVideoByFirsetAndSecond(Integer firstDirectoryId,Integer secondDirectoryId,Integer currentPage,Integer pageSize){
		PageHelper.startPage(currentPage,pageSize);
		//根据视频课程的一级类别id查询其下的每个二级类别id查询其所有的视频课程。
        Page<VideoCourse> videoCourses = videoCourseMapper.selectVideoByFirsetAndSecond(firstDirectoryId, secondDirectoryId);
        return new PageResult(videoCourses.getTotal(),videoCourses.getResult());
	}
	
	/**
	 *	根据输入的搜索内容搜索视频
	 * @param searchContent		用户输入的搜索的内容。
	 * @return
	 */
	public PageResult searchVideo(String searchContent,Integer currentPage,Integer pageSize){
		PageHelper.startPage(currentPage,pageSize);
        Page<VideoCourse> videoCourses = videoCourseMapper.searchVideo(searchContent);
        return new PageResult(videoCourses.getTotal(),videoCourses);
	}
	
	
	/**
	 *	查询免费的视频课程。
	 * @return
	 */
	public PageResult selectFreeVideo(Integer currentPage, Integer pageSize){
		PageHelper.startPage(currentPage,pageSize);
		double commonPrice = 0;//设定普通用户价格为0（免费）
        Page<VideoCourse> videoCourses = videoCourseMapper.selectFreeVideo(commonPrice);//查询普通用户免费的视频
        return new PageResult(videoCourses.getTotal(),videoCourses.getResult());
	}
	/**
	 *	查询vip免费的视频
	 * @return
	 */
	public List<VideoCourse> selectVipFreeVideo(){
		double vipPrice = 0;//设定非普通用户价格为0（免费）
		List<VideoCourse> videoCourses = videoCourseMapper.selectVipFreeVideo(vipPrice);//查询非普通用户免费的视频
		return videoCourses;
	}
	
	/**
	 *	查询最新视频。
	 * @return
	 */
	public PageResult selectNewVideo(Integer currentPage, Integer pageSize){
		//查询最新的视频数据
        PageHelper.startPage(currentPage,pageSize);
        Page<VideoCourse> videoCourses = videoCourseMapper.selectNewVideo();
        return new PageResult(videoCourses.getTotal(),videoCourses.getResult());
	}
	
	/**
	 *	查询最热门的视频
	 * @return
	 */
	public PageResult selectHottestVideo(Integer currentPage, Integer pageSize){
		PageHelper.startPage(currentPage,pageSize);
		//查询sum条最热视频数据
        Page<VideoCourse> videoCourses = videoCourseMapper.selectHottestVideo();
        return new PageResult(videoCourses.getTotal(),videoCourses.getResult());
	}
	
	/**
	 *	查询所有的视频
	 * @return
	 */
	public PageResult selectAllVideo(Integer currentPage, Integer pageSize){
		//查询所有的视频
        PageHelper.startPage(currentPage,pageSize);
        Page<VideoCourse> videoCourses = videoCourseMapper.selectAllVideo();
        return new PageResult(videoCourses.getTotal(),videoCourses.getResult());
	}
	
	
	/**
	 *	通过视频id查询视频详情
	 * @param videoCourseId		视频id
	 * @return
	 */
	public VideoCourse selectByPrimaryKey(Integer videoCourseId) {
		return videoCourseMapper.selectByPrimaryKey(videoCourseId);
	}

    /**
     *	通过视频id查询首页视频详情
     * @param videoCourseId		视频id
     * @return
     */
    public VideoCourse selectHomeByPrimaryKey(Integer videoCourseId) {
        return videoCourseMapper.selectHomeByPrimaryKey(videoCourseId);
    }

	/**
	 *	通过视频id查询状元学堂视频详情
	 * @param videoCourseId		视频id
	 * @return
	 */
	public VideoCourse selectTopSchoolByPrimaryKey(Integer videoCourseId) {
		return videoCourseMapper.selectTopSchoolByPrimaryKey(videoCourseId);
	}

	//搜索精品课程列表
	public PageResult selectCourseBoutique(Integer currentPage, Integer pageSize){
    	PageHelper.startPage(currentPage,pageSize);
		Page<VideoCourse> videoCourses = videoCourseMapper.selectCourseBoutique();
		return new PageResult(videoCourses.getTotal(),videoCourses);
    }
	
	public List<VideoCourse> selectCourseBoutiqueLike(String courseName){
		return videoCourseMapper.selectCourseBoutiqueLike(courseName);
	}
	
	public List<CourseBean> selectFamousSpeaker(){
		return videoCourseMapper.selectFamousSpeaker();
    }
	
	public PageResult selectFSByTeacherName(Integer currentPage,Integer pageSize,String teacherName){
		PageHelper.startPage(currentPage,pageSize);
		Page<VideoCourse> page = videoCourseMapper.selectFSByTeacherName(teacherName);
		return new PageResult(page.getTotal(),page);
    }
	
	//按视频id查视频
	public CourseBean selectFSByVideoCourseId(Integer videoCourseId){
		return videoCourseMapper.selectFSByVideoCourseId(videoCourseId);
    }
	
	//修改video
	public Integer updateVideo(VideoCourse videoCourse){
		return videoCourseMapper.updateByPrimaryKeySelective(videoCourse);
	}
	
    //首页（除名家讲坛外的其他）查询这门课同类别同老师的其他课程
    public List<VideoCourse> selectClassMates(Integer secondDirectoryId,Integer teacherId){
    	return videoCourseMapper.selectClassMates(secondDirectoryId,teacherId);
    }

    //状元学堂（和名家讲坛）查询这门课同类别的其他课程
    public List<VideoCourse> selectTopSchoolClassMates(Integer secondDirectoryId){
        return videoCourseMapper.selectTopSchoolClassMates(secondDirectoryId);
    }

    //首页名家讲坛模块点击进入的列表页
	public PageResult getListBySecondId(Integer secondId,Integer currentPage,Integer pageSize){
		PageHelper.startPage(currentPage,pageSize);
        Page<VideoCourse> listBySecondId = videoCourseMapper.getListBySecondId(secondId);
        return new PageResult(listBySecondId.getTotal(),listBySecondId);
    }
}

























